package com.tank.nio;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: SuQiEr
 * Date:2017/7/31 0031
 * Time: 下午 10:57
 * To change this template use File | Settings | File Templates.
 */
public class SocketChannelTest {

    public static void main(String[] args) throws IOException {

        Selector selector = Selector.open();
        int[] posts = {5001};
        for (int port : posts) {
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.configureBlocking(false);
            ServerSocket socket = serverSocketChannel.socket();
            InetSocketAddress address = new InetSocketAddress(port);
            socket.bind(address);
            
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
            System.out.println("监听端口"+port);
        }
        
        while (true){
            int numbers = selector.select();
            System.out.println("numbers: "+numbers);

            Set<SelectionKey> selectionKeys = selector.selectedKeys();

            System.out.println("selectionKeys:"+selectionKeys);

            Iterator<SelectionKey> iterator = selectionKeys.iterator();

            while (iterator.hasNext()){
                SelectionKey currentSelector = iterator.next();
                //可连接
                if(currentSelector.isAcceptable()){
                    ServerSocketChannel serverSocketChannel =
                            (ServerSocketChannel)currentSelector.channel();
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    socketChannel.configureBlocking(false);
                    socketChannel.register(selector,SelectionKey.OP_READ);
                    iterator.remove();
                    System.out.println("获取客户端连接socketChannel:"+socketChannel);
                //可读
                }else if(currentSelector.isReadable()){
                    SocketChannel socketChannel = (SocketChannel)currentSelector.channel();
                    //分配512字节缓冲
                    ByteBuffer byteBuffer = ByteBuffer.allocate(512);

                    int readBytes = 0;

                    while (true){

                        byteBuffer.clear();

                        int read = socketChannel.read(byteBuffer);



                        if(read <= 0){
                            break;
                        }

                        //反转
                        byteBuffer.flip();

                        //写到
//                        ByteBuffer writeBuf = ByteBuffer.allocate(50);
//                        writeBuf.put("222222".getBytes());
//                        writeBuf.flip();
                        socketChannel.write(byteBuffer);

                        readBytes+=read;
                    }

                    System.out.println("读取到字节长度:"+readBytes);
                    iterator.remove();
                }
            }
        }
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    }
}














